package com.neu.controller;

import cn.hutool.core.bean.BeanUtil;
import com.neu.base.Result;
import com.neu.entity.FpFatherDemand;
import com.neu.entity.dto.DemandDTO;
import com.neu.service.FpFatherDemandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/fp-father-demand")
@Tag(name = "一级需求管理", description = "管理一级需求")
public class FpFatherDemandController {

    @Autowired
    private FpFatherDemandService fpFatherDemandService;

    @GetMapping("/all-with-second")
    public Result listWithSecondDemands() {
        List<Map<String, Object>> data = fpFatherDemandService.listWithSecondDemands();
        return Result.success(data);
    }

    //添加
    @PostMapping("/add")
    @Operation(summary = "添加需求")
    public Result add(@RequestBody FpFatherDemand fpFatherDemand){
        FpFatherDemand fpFatherDemand1 = new FpFatherDemand();
        BeanUtil.copyProperties(fpFatherDemand, fpFatherDemand1);
        Random random = new Random();
        int id = random.nextInt(100000);
        fpFatherDemand.setId(id);
        fpFatherDemandService.save(fpFatherDemand);
        return Result.success(id);
    }

    //搜索需求
    @PostMapping("/search")
    @Operation(summary = "搜索需求及关联的二级需求")

    public Result search(@RequestBody String pname) {
        System.out.println("pname parameter: " + pname);
        // 调用服务层方法进行搜索
        List<Map<String, Object>> result = fpFatherDemandService.searchByPnameWithSecondDemands(pname);
        // 返回查询结果
        return Result.success(result);
    }

    //更新需求
    @PostMapping("/update")
    @Operation(summary = "更新需求")
    public Result update(@RequestBody FpFatherDemand fpFatherDemand){
        return Result.success(fpFatherDemandService.updateById(fpFatherDemand));
    }

    //删除单个需求
    @PostMapping("/delete")
    @Operation(summary = "删除需求")
    public Result delete(@RequestBody FpFatherDemand fpFatherDemand){
        return Result.success(fpFatherDemandService.removeById(fpFatherDemand.getId()));
    }

    //批量删除一级需求
    @PostMapping("/deleteBatch")
    @Operation(summary = "批量删除需求")
    public Result deleteBatch(@RequestBody String[] ids){
        return Result.success(fpFatherDemandService.removeByIds(Arrays.asList(ids)));
    }



}
